<?php 
class Landing_model extends Model
{
	function __construct()
	{
		parent::__construct();
    	$this->load->database();
        $this->region_table='region';	
	}
	
	function digital_print()
	{
		$query = $this->db->get_where('bur_staticpages', array('staticpage_link' => 'digital_print'));
		
		return $query->result();
	}
	
	function litho_print()
	{
	
		$query = $this->db->get_where('bur_staticpages', array('staticpage_link' => 'litho_print'));
		
		return $query->result();
	}
    function _get_region_by_country($country_id){
   // $this->db->where('country', $country_id);
    $this->db->where('adm1code =', 'kyp');
     $this->db->order_by('name');
    $query = $this->db->get($this->region_table);
    return $query->result_array();
    }

    function get_newest_company($limit= 10, $offset = 0, $province = '', $services = '', $search = '', $package_type=''){

        if(!empty($services) && $services != "'all'"){

            $sql_services = "WHERE id_services IN ($services) ";
        }
        $today = date("Y-m-d H:i:s");
        /*$sql = "SELECT d_user.paid_until , d_company.*, region.name as region_name , province_name,
                city.name as city_name_,city_name ";*/
        $sql = "SELECT pt.membership_name, d_user.paid_until , d_company.* ,region.name ";
        $sql .= "FROM directory_company AS d_company ";
        $sql .= "LEFT JOIN directory_user AS d_user ON d_user.id = d_company.id ";
        $sql .= "LEFT JOIN region AS region ON region.id = d_company.region_id ";
        $sql .= "LEFT JOIN directory_package_relationships AS dpr ON dpr.directory_user_id = d_user.id ";
        $sql .= "LEFT JOIN package_offers AS po ON dpr.package_offer_id = po.id ";
        $sql .= "LEFT JOIN package_types AS pt ON po.package_type_id = pt.id ";
        //$sql .= "LEFT JOIN city AS city ON city.id = d_company.city_id ";
        //$sql .= "LEFT JOIN ( SELECT id_company ";
        $sql .= "INNER JOIN ( SELECT id_company ";
        $sql .= "FROM directory_services_relationship ";
        if(!empty($sql_services)){
            $sql .= $sql_services;
        }
        $sql .= "GROUP BY id_company) AS rel ON rel.id_company = d_company.id ";
       $sql .= "WHERE pt.code = '$package_type' ";
        $sql .= "AND dpr.expire_date > '$today' ";
        $sql .= "AND d_user.activation_status ='1' ";
        if((!empty($province)) && $province != 'all_province'){
            $sql_province = "AND region.id = $province ";
            $sql .= $sql_province;
        }
        if(!empty($search)){
            $sql_search = "AND d_company.company LIKE '%$search%' ";
            $sql .= $sql_search;
        }
        $sql .= "ORDER BY rand() ";
       $sql .= "LIMIT $offset, $limit";
       //echo $sql."<br>";
        $query = $this->db->query($sql);
        //echo ($this->db->last_query());
        if ($query->num_rows() > 0) {
            $result = $query->result_array(); //print_r($result);
            return $result;
            //return $this->db->last_query();
        } else {
            return false;
        }
  }
    
}
?>